﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BullTasDesaDal.Service;
using BullTasDesaDal.Entities;
using System.Globalization;

namespace WSBulTasDesa
{
    public partial class wfAppraisal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                ucLocation.Title = "Ubicación";
                ucLocation.Distance = "Distancia Capital";
                ucLocationPC.Title = "Capital mas cercana";
                ucLocationPC.Distance = "Distancia";
                ucLocationPL.Title = "Localidad mas cercana";
                ucLocationPL.Distance = "Distancia";

                txtRevisionDate.Attributes.Add("readonly", "readonly");

                chkRevisor.DataSource = CachingHelper.LoadRevisors();
                chkRevisor.DataValueField = "Id";
                chkRevisor.DataTextField = "Name";
                chkRevisor.DataBind();

                if (!string.IsNullOrEmpty(Request.QueryString["idApprasisal"]))
                {
                    this.ShowAppraisal(AppraisalService.GetById(Convert.ToInt32(Request.QueryString["idApprasisal"])));
                }

            }
        }

        protected void btnHidden_Click(object sender, EventArgs e)
        {
            Save();

        }

        protected void Save()
        {
            Appraisal appraisal = new Appraisal();
            if (!string.IsNullOrEmpty(Request.QueryString["idApprasisal"]))
                appraisal.Id = Convert.ToInt32(Request.QueryString["idApprasisal"]);

            appraisal.EstablishmentName = txtEstablishmentName.Text;
            appraisal.Owner = txtOwner.Text;
            appraisal.Contact = txtContact.Text;
            appraisal.OwnerTel = txtOwnerTel.Text;
            appraisal.OwnerMail = txtOwnerMail.Text;
            appraisal.CatastralNomenclature = txtCatastralNomenclature.Text;
            appraisal.FolderNumber = txtFolderNumber.Text;
            appraisal.Ha = Convert.ToDecimal(txtHa.Text, CultureInfo.InvariantCulture.NumberFormat);
            appraisal.DistanceBA = Convert.ToInt16(txtDistanceBA.Text);
            appraisal.RevisionDate = Convert.ToDateTime(txtRevisionDate.Text);
            appraisal.Revisors = this.GetRevisors();

            appraisal.DistanceCapital = ucLocation.DistanceKm; ;
            appraisal.DistancePC = ucLocationPC.DistanceKm;
            appraisal.DistancePL = ucLocationPL.DistanceKm;

            appraisal.Zone = ucRegion.Zone;
            appraisal.Locality = ucLocation.Locality;
            appraisal.LocalityPC = ucLocationPC.Locality;
            appraisal.LocalityPL = ucLocationPL.Locality;

            AppraisalService.Save(appraisal);
            extMsg.Show();
        }

        protected IList<Revisor> GetRevisors()
        {
            IList<Revisor> lrevisor = new List<Revisor>();
            for (int i = 0; i < chkRevisor.Items.Count; i++)
            {
                if (chkRevisor.Items[i].Selected)
                {
                    Revisor revisor = new Revisor();
                    revisor.Id = chkRevisor.Items[i].Value;
                    revisor.Name = chkRevisor.Items[i].Text;
                    lrevisor.Add(revisor);
                }

            }
            return lrevisor;
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfAppraisal.aspx");
        }

        protected void ShowAppraisal(Appraisal app)
        {
            txtEstablishmentName.Text = app.EstablishmentName;
            txtCatastralNomenclature.Text = app.CatastralNomenclature;
            txtFolderNumber.Text = app.FolderNumber;
            txtOwner.Text = app.Owner;
            txtContact.Text = app.Contact;
            txtOwnerTel.Text = app.OwnerTel;
            txtOwnerMail.Text = app.OwnerMail;
            txtHa.Text = app.Ha.ToString(CultureInfo.InvariantCulture.NumberFormat);
            txtDistanceBA.Text = app.DistanceBA.ToString();
            calRevisionDate.SelectedDate = app.RevisionDate;
            
            foreach (Revisor revisor in app.Revisors)
            {
                for (int i = 0; i < chkRevisor.Items.Count; i++)
                {
                    if (chkRevisor.Items[i].Value == revisor.Id)
                        chkRevisor.Items[i].Selected = true;
                }
            }

            ucRegion.Zone = app.Zone;
            ucLocation.Locality = app.Locality;
            ucLocationPC.Locality = app.LocalityPC;
            ucLocationPL.Locality = app.LocalityPL;

            ucLocation.DistanceKm = app.DistanceCapital;
            ucLocationPC.DistanceKm = app.DistancePC;
            ucLocationPL.DistanceKm = app.DistancePL;

        }

       
    }
}